package demo.practice.bucket;

import java.util.HashMap;
import java.util.Map;

public class P3_2 {

    public static void main(String[] args) {
        P3_2 p3_2 = new P3_2();
        p3_2.lengthOfLongestSubstring("ohomm");
    }

    public int lengthOfLongestSubstring(String s) {
        int max = 0;
        int indexWindowsLeft = 0;
        Map<Character, Integer> map = new HashMap<>();

        int j = indexWindowsLeft;
        while (j < s.length()) {
            char c = s.charAt(j);
            if (map.containsKey(c)) {
                int index = map.get(c);
                while (indexWindowsLeft <= index) {
                    map.remove(s.charAt(indexWindowsLeft++));
                }
            } else {
                map.put(c, j);
                max = Math.max(map.size(), max);
                j++;
            }
        }
        return max;
    }
}
